package com.wyl.aop;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.wyl.model.Access;
import com.wyl.service.AccessService;

@Aspect
@Component
public class AccessRecordAspect {

	private Logger log = LoggerFactory.getLogger(getClass());

	@Autowired
	private AccessService accessService;
	
	@Pointcut("execution(public * com.wyl.controller..*.*(..))")
	public void access(){};
	
	@Before("access()")
	public void before(JoinPoint point)throws Throwable {
		ServletRequestAttributes attributes  = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
		HttpServletRequest request = attributes.getRequest();
		
		Access a = new Access();
		a.setTime(new Date());
		a.setIp(request.getRemoteAddr());
		a.setUrl(request.getRequestURI());
		accessService.save(a);
		log.info("RemoteAddr:"+request.getRemoteAddr());
		log.info("url:"+request.getMethod());

		
	}
	
}
